// #!/usr/bin/groovy
// @Library('github.com/fabric8io/fabric8-pipeline-library@master')
// import com.gitee.infrastlabs

// def utils = new com.gitee.infrastlabs.Utils()
// def envStage = utils.environmentNamespace('stage')

def pipe(vlabel){
    def icos= "\u26F0" //montain
    def icot= "\u261B" //pointRightBlack +

	node(vlabel) {ansiColor('xterm') {
		stage(icos+'SrcAnay'){ container('maven'){ 
			task(icot+'pacakge')
			echo "pkg"
			//sh "export |grep SEL_ && export |grep JAVA"
			//sh "cd /root/.jenkins_ci/_slave/maven; sh rpc_provider.sh xshell"
		}}
		stage(icos+'BuildPack'){ container('maven'){ 
			task(icot+'pacakge')
			echo "pkg"
			//sh "export |grep SEL_ && export |grep JAVA"
			//sh "cd /root/.jenkins_ci/_slave/maven; sh rpc_provider.sh xshell"
		}}
		stage(icos+'TagMark'){ 
			task(icot+'git-tag')
			container('maven'){ echo "pkg" }
			//sh "export |grep SEL_ && export |grep JAVA"
			//sh "cd /root/.jenkins_ci/_slave/maven; sh rpc_provider.sh xshell"

			task(icot+'ftp-mark')
			container('maven'){ echo "ftp-mark" }
			task(icot+'ftp-mark')
			container('maven'){ echo "ftp-mark" }
			task(icot+'ftp-mark')
			container('maven'){ echo "ftp-mark" }
			task(icot+'ftp-mark')
			container('maven'){ echo "ftp-mark" }
			task(icot+'ftp-mark')
			container('maven'){ echo "ftp-mark" }
			task(icot+'ftp-mark')
			container('maven'){ echo "ftp-mark" }
			task(icot+'ftp-mark')
			container('maven'){ echo "ftp-mark" }
			task(icot+'ftp-mark')
			container('maven'){ echo "ftp-mark" }
			task(icot+'ftp-mark')
			container('maven'){ echo "ftp-mark" }
			task(icot+'ftp-mark')
			container('maven'){ echo "ftp-mark" }
			task(icot+'ftp-mark')
			container('maven'){ echo "ftp-mark" }
			task(icot+'ftp-mark')
			container('maven'){ echo "ftp-mark" }
			task(icot+'ftp-mark')
			container('maven'){ echo "ftp-mark" }

			echo "ftp-mark"
		}

		stage(icos+'EnvDeploy'){
		container('docker'){ 
			task(icot+'docker-img-build')
			//sh "cd /root/.jenkins_ci/_slave/docker; sh rpc_provider.sh xshell"
			echo "docker-img-build"

		}
		container('kubectl'){
			task(icot+'k8s deploy')
			//sh "sed -i \"s/k-app\\/k8s-example/${params.REGISTRY_URL}\\/k-app\\/k8s-example:${shortGitCommit}/g\" k8s-example.yaml"
			//sh "kubectl --kubeconfig=/root/.kube/config get pod --all-namespaces"
			//sh "kubectl --kubeconfig=/root/.kube/config apply -f /opt/shell/scripts/_slave/kubectl/tpl/rpc.yml"
			echo "kubectl"
			sh "sleep 60"
		}}



	}}
}


def deploy(id) {
    def vlabel = "k3s-${UUID.randomUUID().toString().substring(0,4)}"
    //def vlabel = "k3s-ci-sboot-demo2"
    def utils = new com.gitee.infrastlabs.Utils()
    ret = utils.getEmpSlave("k3s-")
    if ( ret.length() != 0) {
		vlabel = ret
    }
    //, readOnly: true
    podTemplate(label: vlabel, cloud: 'kubernetes', 
			containers: [
              //containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-slave:alpine', args: '${computer.jnlpmac} ${computer.name}'),
			  containerTemplate(name: 'jnlp', image: 'registry.cn-shenzhen.aliyuncs.com/k-spe/dvp-jnlp', alwaysPullImage: true, args: '${computer.jnlpmac} ${computer.name}'),
			  containerTemplate(name: 'git', image: 'alpine/git', alwaysPullImage: true, command: 'cat', ttyEnabled: true),
			  containerTemplate(name: 'maven', image: 'registry.cn-shenzhen.aliyuncs.com/k-spe/dvp-maven', alwaysPullImage: true, command: 'cat', ttyEnabled: true),
              containerTemplate(name: 'docker', image: 'registry.cn-shenzhen.aliyuncs.com/k-spe/dvp-docker', alwaysPullImage: true, command: 'cat', ttyEnabled: true),
              containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl:v1.10.2', command: 'cat', ttyEnabled: true)
			],
          volumes: [
				//hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock'),
				//hostPathVolume(hostPath: '/root/.kube', mountPath: '/root/.kube'),
				//hostPathVolume(hostPath: '/root/.m2', mountPath: '/root/.m2'),
				//persistentVolumeClaim(claimName: 'jenkins-shell', mountPath: '/opt/shell'),
				//persistentVolumeClaim(claimName: 'jenkins-jk-ci-data', mountPath: '/opt/jk_ci_data')
				hostPathVolume(hostPath: '/opt/jenkins/shell', mountPath: '/opt/shell'),
				hostPathVolume(hostPath: '/opt/jenkins/jk_ci_data', mountPath: '/opt/jk_ci_data')
            ],
          imagePullSecrets: [ 'regsecret' ],
          idleMinutes: 600,
          annotations: [
				podAnnotation(key: "sidecar.istio.io/inject", value: "false")
           ]
    ) { this.pipe(vlabel) }  
}

def call() {
    // def utils = new com.gitee.infrastlabs.Utils()
    deploy('aa')
    //return this
}